Ext.application({
    name: 'App',
    extend: 'Extend.app.AjaxSecurityApplication',
    appFolder: 'app',
    requires: [
        'App.InitView',
        'App.' + (Ext.isEmpty(role) == true ? 'main' : ('management.' + role)) + '.view.Viewport', 'App.commons.grid.column.KpiColumn'],
    mainViewClass: 'App.' + (Ext.isEmpty(role) == true ? 'main' : ('management.' + role)) + '.view.Viewport',
    mainView: 'App.InitView',
    init: function (application) {

        // Extend.Utils.getValueByPath({a: {b: {c: 111}}}, 'a.b.c');

        Ext.log.info('development:true');
        Ext.log.info('libsPath:' + libsPath);
        Ext.log.info('extjsPath:' + extjsPath);
        Ext.log.info('extendPath:' + extendPath);
        Ext.log.info('theme:' + theme);

        window.app = window.App = window.Application = window.application = application;
        window.app.optsQueue = [];
        Ext.Ajax.disableCachingParam = 'dc';

        this.custominit();
        //this.checkBrowser();
        var me = this;
        Ext.Ajax.request({
            url: 'load_initial_information',
            success: function (resp) {
                var result = resp.result;
                app.localhost = result.data.localhost;
                app.user = result.data.user;
                app.project = result.data.project;
                if (me.getMainView().isMasked()) {
                    me.getMainView().unmask();
                }
                me.getMainView().destroy();
                me.setMainView(me.mainViewClass);
                Ext.log({
                    level: 'info',
                    msg: app.localhost
                })
            }
        });
        window.onbeforeunload = function () {
            if (!Ext.Object.isEmpty(app.editors)) {
                return "存在编辑中的数据还没有保存，是否要离开此页面？";
            }
            return null;
        }


    },
    showLoginWindow: function () {
        Ext.create('App.authorization.user.view.LoginWindow', {}).show();
    },
    checkBrowser: function () {
        if ((Ext.browser.name == 'Chrome' && Ext.browser.version.major < 42)
            || (Ext.browser.name == 'IE' && Ext.browser.version.major < 10)) {
            Extend.Msg
                .warn(
                    '提示',
                    '为了更好的用户体验，建议浏览器版本：Google chrome 42.xxx及以上版本、IE10及以上版本。<br/><br/>'
                    + '<span>Google Chrome：<a style="color:blue;" href="software/ChromeStandalone_49.0.2623.110_Setup.1459233395.exe">ChromeStandalone_49.0.2623.110_Setup.1459233395</a></span><br/>'
                    + 'Internet Explorer 11 32位：<a  style="color:blue;" href="software/Internet Explorer11_ ForWin7_32bit_201610.exe">Internet Explorer 11 for Win7_32bit</a><br/>'
                    + 'Internet Explorer 11 64位：<a  style="color:blue;" href="software/Internet Explorer11_ ForWin7_64bit_201610.exe">Internet Explorer 11 for Win7_64bit</a>');
        }
    }
    ,
    checkAuthorities: function (view) {
        var mainView = this.getMainView();
        var target = view || mainView;
        var list = target.query('component[security="true"]');
        if (Ext.isEmpty(list) || list.length == 0) {
            return;
        }
        if (Ext.isEmpty(this.user) || Ext.isEmpty(app.user.resources)) {
            Ext.Array.each(list, function (item) {
                item.hide();
            })
        } else {
            Ext.Array.each(list, function (item) {
                var uiid = item.uiid;
                var authorities = Ext.Array.findBy(app.user.resources,
                    function (resource) {
                        if (!Ext.isEmpty(uiid.match(resource.uiid))) {
                            return item;
                        }
                    })
                if (Ext.isEmpty(authorities)) {
                    item.hide();
                } else {
                    item.show();
                }
            })
        }
    }
    ,
    custominit: function () {
        window.onkeydown = function (e) {
            if (event.keyCode == 8) {
                if (e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA"
                    && e.target.className != "dotitem") {
                    if (app.app.optsQueue.length > 1) {
                        var lastIndex = app.app.optsQueue.length - 2;
                        var item = app.app.optsQueue[lastIndex];
                        Ext.Array.removeAt(app.app.optsQueue, lastIndex, 10);
                        app.app._mainView.down('North').fireEvent('menuclick',
                            item.clazz, item.options)
                    }
                    console.log(app.app._mainView)
                    if (Ext.isFunction(e.stopEvent)) {
                        e.stopEvent();
                    }
                    if (Ext.isFunction(e.preventDefault)) {
                        e.preventDefault();
                    }
                }
            }
        };

        //处理svg引擎初始化图时，出现:MNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaN错误日志
        Ext.override(Ext.draw.engine.Svg, {
            setElementAttributes: function (element, attributes) {
                var dom = element.dom,
                    cache = element.cache,
                    name, value;
                for (name in attributes) {
                    value = attributes[name];
                    if (cache[name] !== value) {
                        cache[name] = value;
                        if (Ext.String.startsWith(value + "", 'MNaN,')) {
                            continue;
                        }
                        dom.setAttribute(name, value);
                    }
                }
            }
        })
    },
    hasRole: function (role) {
        if (Ext.isEmpty(app.user) || Ext.isEmpty(app.user.roles) || app.user.roles.length <= 0) {
            return false;
        }
        return Ext.Array.contains(app.user.roles, role);
    }
})

window.KpiUtil = {};
window.KpiUtil.judgmentState = function (record, format, cc, val) {
    if (record.get('迭代') != '基线' && record.get('迭代') != '目标') {
        var html = '<div style="height: 100%;width: 100%;" title="{0}"><img src="css/icons/{1}" style="margin-right:5px;"/>{2}</div>';
        var title = [];

        if (!Ext.isEmpty(cc.baselineValue)) {
            title.push("基线值：" + !Ext.isEmpty(format) ? format(cc.baselineValue, me.rendererScope || me.resolveListenerScope()) : cc.baselineValue);
        }
        if (!Ext.isEmpty(cc.maxValue)) {
            title.push("目标值：" + !Ext.isEmpty(format) ? format(cc.targetValue, me.rendererScope || me.resolveListenerScope()) : cc.targetValue);
        }
        if (!Ext.isEmpty(cc.minValue)) {
            title.push("最小值：" + !Ext.isEmpty(format) ? format(cc.minValue, me.rendererScope || me.resolveListenerScope()) : cc.minValue);
        }
        if (!Ext.isEmpty(cc.maxValue)) {
            title.push("最大值：" + !Ext.isEmpty(format) ? format(cc.maxValue, me.rendererScope || me.resolveListenerScope()) : cc.maxValue);
        }
        if (!Ext.isEmpty(cc.minValue) && value < cc.minValue) {
            var tmp = Ext.String.format(html, title.join("，"), 'triangle-red-down.png', val);
            return tmp;
        }
        if (!Ext.isEmpty(cc.maxValue) && value > cc.maxValue) {
            return Ext.String.format(html, title.join("，"), 'triangle-red-up.png', val);
        }
        if (!Ext.isEmpty(cc.minValue) || !Ext.isEmpty(cc.maxValue)) {
            return Ext.String.format(html, title.join("，"), 'circular-green.png', val);
        }
    }
}
